// components/Login.vue
<template>
  <div class="login-container">
    <div class="login-box">
      <h1>登录</h1>
      <form @submit.prevent="loginFn">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="username" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入密码"
            required
          />
        </div>
        <button type="submit" class="login-button">登录</button>
      </form>
      <p class="register-link">还没有账号？<a href="#">注册</a></p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { loginAPI } from '@/aip/loginAPI'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { md5 } from 'js-md5' // 引入md5

const router = useRouter()
const username = ref('concord_zhouqi')
const password = ref('!sunnywing0')

const loginFn = async () => {
  try {
    console.log('正在登录...')
    // 获取当前时间作为时间戳
    const now = new Date()
    const year = now.getFullYear()
    const month = String(now.getMonth() + 1).padStart(2, '0')
    const day = String(now.getDate()).padStart(2, '0')
    const hours = String(now.getHours()).padStart(2, '0')
    const minutes = String(now.getMinutes()).padStart(2, '0')
    const seconds = String(now.getSeconds()).padStart(2, '0')
    const login_date = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    const time = md5(login_date)
    const varification_code = '123456'
    // 生成 keycode
    const keycode = md5(username.value + password.value + 'tiandagonghe' + time)

    // 传入用户名、密码、keycode和时间戳
    const res = await loginAPI(
      username.value,
      password.value,
      login_date,
      keycode,
      varification_code,
    )
    console.log(res)
    console.log(username.value, password.value, login_date)
    // console.log(login_date)
    if (res.data.status === 200) {
      // 直接使用res.code判断
      // 登录成功，存储token
      localStorage.setItem('token', res.data.data.token)
      localStorage.setItem('username', username.value)
      localStorage.setItem('userid', res.data.data.userid)
      // 显示成功消息
      ElMessage.success('登录成功')
      // 跳转到首页
      router.push('/')
    } else {
      // 登录失败
      //   ElMessage.error(res.data.msg || '登录失败，请检查用户名和密码') // 直接使用res.message
    }
  } catch (error) {
    console.error('登录出错:', error)
    ElMessage.error('登录失败，请检查网络连接或联系管理员')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 确保容器占据整个视口高度 */
  background-image: url(./images/loginBg.png);
  background-repeat: no-repeat;
  background-size: cover;
}

.login-box {
  background-color: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 400px;
  text-align: center;
}

h1 {
  margin-bottom: 20px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #555;
}

input[type='text'],
input[type='password'] {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box; /* 确保 padding 不会增加元素的总宽度 */
  font-size: 16px;
}

input[type='text']:focus,
input[type='password']:focus {
  outline: none;
  border-color: #4caf50; /* 绿色边框 */
  box-shadow: 0 0 5px rgba(76, 175, 80, 0.5); /* 绿色阴影 */
}

.login-button {
  background-color: #4caf50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  width: 100%;
  font-size: 18px;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #3e8e41;
}

.register-link {
  margin-top: 20px;
  color: #777;
}

.register-link a {
  color: #4caf50;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>
